﻿on_game_start_after_lobby = {
	#Cannot be on_actions
	first_valid_on_action = {
		calyver_game_start_all_on_actions
	}
}

calyver_game_start_all_on_actions = {
	on_actions = {
		calyver_set_doctrines
		calyver_consort_favor_valuation_on_action
		calyver_mulsim_add_court_eunuchs_on_action
	}
}

calyver_set_doctrines = {
	effect = {
		every_religion_global = {
			every_faith = {
				if = {
					limit = {
						has_doctrine = doctrine_monogamy
						NOT = {
							has_variable = doctrines_set
						}
					}
					remove_doctrine = doctrine_monogamy
					add_doctrine = doctrine_monogamy
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_concubines_0
								has_doctrine = doctrine_concubines_3
								has_doctrine = doctrine_concubines_10
								has_doctrine = doctrine_concubines_30
								has_doctrine = doctrine_concubines_50
								has_doctrine = doctrine_concubines_100
								has_doctrine = doctrine_concubines_130
							}
						}
						add_doctrine = doctrine_concubines_0
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_marriage_realm_celebration_disallowed
								has_doctrine = doctrine_marriage_realm_celebration_first
							}
						}
						add_doctrine = doctrine_marriage_realm_celebration_first
					}	
					set_variable = {
						name = doctrines_set
						days = 2
					}
				} 
				else_if = {
					limit = {
						has_doctrine = doctrine_concubines
						NOT = {
							has_variable = doctrines_set
						}
					}
					remove_doctrine = doctrine_concubines
					if= {
						limit = {
							NOR = {
								has_doctrine = doctrine_monogamy
								has_doctrine = doctrine_polygamy_4
								has_doctrine = doctrine_polygamy_10
								has_doctrine = doctrine_polygamy_15
								has_doctrine = doctrine_polygamy_25
								has_doctrine = doctrine_polygamy_50
								has_doctrine = doctrine_polygamy_100
							}
						}
						if = {
							limit = {
								OR = {	
									has_game_rule = calyver_harem_concubinage
									has_game_rule = calyver_harem_concubinage_10
								}
							}
							add_doctrine = doctrine_polygamy_4
						}
						else = {
							add_doctrine = doctrine_monogamy
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_concubines_0
								has_doctrine = doctrine_concubines_3
								has_doctrine = doctrine_concubines_10
								has_doctrine = doctrine_concubines_30
								has_doctrine = doctrine_concubines_50
								has_doctrine = doctrine_concubines_100
								has_doctrine = doctrine_concubines_130
							}
						}
						if = {
							limit = {
								OR = {	
									has_game_rule = calyver_expanded_concubinage
									has_game_rule = calyver_harem_concubinage_10
								}
							}
							add_doctrine = doctrine_concubines_10
						}
						else = {
							add_doctrine = doctrine_concubines_3
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_marriage_realm_celebration_disallowed
								has_doctrine = doctrine_marriage_realm_celebration_first
							}
						}
						add_doctrine = doctrine_marriage_realm_celebration_disallowed
					}	
					set_variable = {
						name = doctrines_set
						days = 2
					}				
				}
				else_if = {
					limit = {
						has_doctrine = doctrine_polygamy
						NOT = {
							has_variable = doctrines_set
						}
					}
					remove_doctrine = doctrine_polygamy
					if= {
						limit = {
							NOR = {
								has_doctrine = doctrine_monogamy
								has_doctrine = doctrine_polygamy_4
								has_doctrine = doctrine_polygamy_10
								has_doctrine = doctrine_polygamy_15
								has_doctrine = doctrine_polygamy_25
								has_doctrine = doctrine_polygamy_50
								has_doctrine = doctrine_polygamy_100
							}
						}
						if = {
							limit = {
								has_game_rule = calyver_expanded_polygamy
							}
							add_doctrine = doctrine_polygamy_10
						}
						else = {
							add_doctrine = doctrine_polygamy_4
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_concubines_0
								has_doctrine = doctrine_concubines_3
								has_doctrine = doctrine_concubines_10
								has_doctrine = doctrine_concubines_30
								has_doctrine = doctrine_concubines_50
								has_doctrine = doctrine_concubines_100
								has_doctrine = doctrine_concubines_130
							}
						}
						if = {
							limit = {
								has_game_rule = calyver_harem_polygamy
							}
							add_doctrine = doctrine_concubines_3
						}
						else_if = {
							limit = {
								has_game_rule = calyver_harem_polygamy_10
							}
							add_doctrine = doctrine_concubines_10
						}
						else = {
							add_doctrine = doctrine_concubines_0
						}
					}
					if = {
						limit = {
							NOR = {
								has_doctrine = doctrine_marriage_realm_celebration_disallowed
								has_doctrine = doctrine_marriage_realm_celebration_first
							}
						}
						add_doctrine = doctrine_marriage_realm_celebration_disallowed
					}	
					set_variable = {
						name = doctrines_set
						days = 2
					}	
				}
			}
		}		
	}
}

calyver_consort_favor_valuation_on_action = {
	events = {
		consort_favor_valuation.0001	#Favor system
	}
}

calyver_mulsim_add_court_eunuchs_on_action = {
	effect = {
		every_culture_global = {
			limit = {
				culture_head ?= {
					religion = religion:islam_religion
				}
				NOR = {
					has_cultural_parameter = can_castrate_prisoners
					has_cultural_parameter = can_appoint_chief_eunuch
					has_cultural_parameter = family_castration
				}
			}
			add_culture_tradition = tradition_court_eunuchs
		}		
	}
}